﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
using Portal.Environment;
using Portal.Language;
using Portal.Interface;

namespace Portal.CDSP
{
    public class MagSuggestWP : WebPart
    {
        public override string WebPartId
        {
            get
            {
                return "WebSite.MagSuggestWP";
            }
        }

        public override bool IsMainWebPart
        {
            get
            {
                return true;
            }
        }

        public override string Title
        {
            get
            {
                return ResourceBLL.GetText(WebPartId, "Title", "QL Nội dung góp ý Website");
            }
        }

        public override string Description
        {
            get
            {
                return ResourceBLL.GetText(WebPartId, "Description", "");
            }
        }

        public override WebPartInt[] SubWebParts
        {
            get
            {
                WebPart[] SubWebParts = new WebPart[]{
                    new SuggestDetailWP()
                };
                return SubWebParts;
            }
        }


        public override string RenderWebPart(LoadParamCls loadParamCls)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(MagSuggestWP));

            string RetHtml = "" +
                Portal.Environment.WebPartUtilities.AttachJavascript("Portal.CDSP", "feedback.js") +
                "<div id='FeedBackList'>" +
                RenderListContact() +
                "</div>" +
                ""
                ;
            return RetHtml;
        }
        [AjaxPro.AjaxMethod()]
        public string RenderListContact()
        {
            string RetHtml = string.Empty;

            DataTable ListFeedBack = new ContactBLL().SelectAll();
            if (ListFeedBack.Rows.Count <= 0) return ResourceBLL.GetText(WebPartId, "SG_NoRecordFound", "Chưa có góp ý nào!");

            /* Header */
            RetHtml =
                "   <table>" +
                "       <tr>" +
                "           <th>#</th>" +
                "           <th style= display:none>" + ResourceBLL.GetText(WebPartId, "SG_Id", "SuggestID") + "</th>" +
                "           <th>" + ResourceBLL.GetText(WebPartId, "SG_Subject", "Họ Tên") + "</th>" +
                "           <th>" + ResourceBLL.GetText(WebPartId, "SG_Name", "Tiêu đề") + "</th>" +

                "           <th>" + ResourceBLL.GetText(WebPartId, "SG_Address", "Địa chỉ") + "</th>" +
                "           <th>" + ResourceBLL.GetText(WebPartId, "SG_Phone", "Điện thoại") + "</th>" +
                "           <th>" + ResourceBLL.GetText(WebPartId, "SG_Email", "Email") + "</th>" +
                "           <th>" + ResourceBLL.GetText(WebPartId, "SG_View", "Xem") + "</th>" +
                "           <th>" + ResourceBLL.GetText(WebPartId, "SG_Delete", "Xóa") + "</th>" +
                "       </tr>";

            /* Body */
            for (int iIndex = 0; iIndex < ListFeedBack.Rows.Count; iIndex++)
            {
                string SuggestId = "";
                try
                {
                    SuggestId = ListFeedBack.Rows[iIndex]["SuggestId"].ToString();
                }
                catch { }
                string Name = "";
                try
                {
                    Name = ListFeedBack.Rows[iIndex]["SuggestBy"].ToString();
                }
                catch { }
                string Subject = "";
                try
                {
                    Subject = ListFeedBack.Rows[iIndex]["Subject"].ToString();
                }
                catch { }
                string Email = "";
                try
                {
                    Email = ListFeedBack.Rows[iIndex]["Email"].ToString();
                }
                catch { }
                string Address = "";
                try
                {
                    Address = ListFeedBack.Rows[iIndex]["Address"].ToString();
                }
                catch { }
                string Phone = "";
                try
                {
                    Phone = ListFeedBack.Rows[iIndex]["Phone"].ToString();
                }
                catch { }

                RetHtml +=
                "<tr onMouseOver=\"this.className='highlight'\" onMouseOut=\"this.className='normal'\">" +
                "   <td valign=top class=cssTdIndex>" + (iIndex + 1) + "</td>" +
                "   <td valign=top class=cssTdSubject>" + Name + "</td>" +
                "   <td valign=top class=cssTdSubject>" + Subject + "</td>" +
                "   <td valign=top class=cssTdSubject>" + Address + "</td>" +
                "   <td valign=top class=cssTdSubject>" + Phone + "</td>" +
                "   <td valign=top class=cssTdSubject>" + Email + "</td>" +
                "   <td valign=top><a href=\"" + "javascript:ClientViewFeedBack('" + SuggestId + "');" + "\">" + ResourceBLL.GetText(WebPartId, "SG_Update", "Xem") + "</a></td>" +
                "   <td valign=top><a href=\"" + "javascript:ClientDeleteFeedBack('" + SuggestId + "');" + "\">" + ResourceBLL.GetText(WebPartId, "SG_Delete", "Xóa") + "</a></td>" +
                "   <tr>";
            }

            RetHtml += "</table>";

            return RetHtml;
        }


        [AjaxPro.AjaxMethod()]
        public string ServerSideDeletetFeedBack(string SuggestID)
        {
            try
            {
                Environments.CheckSessionTimeOut(true);

                SqlTransaction sqlTransaction = Environments.CreateTransaction();
                try
                {
                    new ContactBLL().DeleteSuggest(sqlTransaction, SuggestID);
                    sqlTransaction.Commit();
                }
                catch (Exception ex)
                {
                    sqlTransaction.Rollback();
                    throw (ex);
                }
                return ResourceBLL.GetText(WebPartId, "DeleteOkMessage", "Xóa thành công!");
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string GetViewFeedBackUrl(string SuggestID)
        {
            try
            {
                //PermissionTypeCls PermissionTypeCls = FindPermissionType("ContactsManager");
                //Environment.Permission.CheckPermission(Environment.Environments.CurrentLoginUser.LoginName, PermissionTypeCls.PermissionTypeCode, "AddContact", WebPartId, null, true);
                return Screen.BuildDialogUrl(new ParamCls[]{
                        new ParamCls("WebPartId",new SuggestDetailWP().WebPartId),
                        new ParamCls("SuggestID",SuggestID)
                    });
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod()]
        public string GetConfirmDeleteMsg()
        {
            return ResourceBLL.GetText(WebPartId, "ConfirmDeleteMsg", "Bạn đã chắc chắn xóa chưa?");
        }

        public override bool AllowFrontService
        {
            get
            {
                return false;
            }
        }
    }
}
